package top.ytazwc.principle.openclose;

/**
 * @author 花木凋零成兰
 * @title Ocp
 * @date 2024/4/17 20:05
 * @package top.ytazwc.principle.openclose
 * @description TODO
 */
public class Ocp {
    public static void main(String[] args) {
        // 使用功能 查看存在的问题
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Circle());
        graphicEditor.drawShape(new Triangle());
    }
}
// 这是一个用于绘图的类 [使用方]
class GraphicEditor {
    // 接收Shape对象 然后根据type 绘制不同的图形
    public void drawShape(Shape s) {
        if (s.m_type == 1) {
            drawRectangle(s);
        } else if (s.m_type == 2) {
            drawCircle(s);
        } else if (s.m_type == 3) {
            drawTriangle(s);
        }
    }
    public void drawRectangle(Shape r) {
        System.out.println("绘制矩形");
    }
    public void drawCircle(Shape r) {
        System.out.println("绘制圆形");
    }
    public void drawTriangle(Shape t) {
        System.out.println("绘制三角形");
    }
}
class Shape {
    int m_type;
}
class Rectangle extends Shape {
    Rectangle() {
        super.m_type = 1;
    }
}
class Circle extends Shape {
    Circle() {
        super.m_type = 2;
    }
}
// 新增一个三角形类
class Triangle extends Shape {
    Triangle() {
        super.m_type = 3;
    }
}
